<?php

namespace backend\modules\supplier\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\supplier\models\PurchaseIn;

/**
 * PurchaseInSearch represents the model behind the search form about `backend\modules\supplier\models\PurchaseIn`.
 */
class PurchaseInSearch extends PurchaseIn
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'factory_id', 'add_id', 'add_time', 'chk_id', 'chk_date', 'pay_id', 'pay_date', 'status', 'supplier_id'], 'integer'],
            [['sn', 'factory_name', 'remark'], 'safe'],
            [['total_price', 'total_price_in', 'total_paid'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = PurchaseIn::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'factory_id' => $this->factory_id,
            'total_price' => $this->total_price,
            'total_price_in' => $this->total_price_in,
            'total_paid' => $this->total_paid,
            'add_id' => $this->add_id,
            'add_time' => $this->add_time,
            'chk_id' => $this->chk_id,
            'chk_date' => $this->chk_date,
            'pay_id' => $this->pay_id,
            'pay_date' => $this->pay_date,
            'status' => $this->status,
            'supplier_id' => $this->supplier_id,
        ]);

        $query->andFilterWhere(['like', 'sn', $this->sn])
            ->andFilterWhere(['like', 'factory_name', $this->factory_name])
            ->andFilterWhere(['like', 'remark', $this->remark]);

        return $dataProvider;
    }
}
